package com.fruitshop.controller;

import com.fruitshop.dto.AddressDTO;
import com.fruitshop.entity.Address;
import com.fruitshop.service.AddressService;
import com.fruitshop.vo.ResultVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址控制器
 */
@Slf4j
@RestController
@RequestMapping("/address")
@RequiredArgsConstructor
public class AddressController {
    
    private final AddressService addressService;
    
    /**
     * 添加收货地址
     *
     * @param request    HTTP请求
     * @param addressDTO 地址数据
     * @return 地址ID
     */
    @PostMapping("/add")
    public ResultVO<Long> add(HttpServletRequest request, @RequestBody @Valid AddressDTO addressDTO) {
        Long userId = (Long) request.getAttribute("userId");
        Long addressId = addressService.add(userId, addressDTO);
        return ResultVO.success(addressId);
    }
    
    /**
     * 修改收货地址
     *
     * @param request    HTTP请求
     * @param addressDTO 地址数据
     * @return 修改结果
     */
    @PutMapping("/update")
    public ResultVO<Boolean> update(HttpServletRequest request, @RequestBody @Valid AddressDTO addressDTO) {
        Long userId = (Long) request.getAttribute("userId");
        Boolean result = addressService.update(userId, addressDTO);
        return ResultVO.success(result);
    }
    
    /**
     * 删除收货地址
     *
     * @param request   HTTP请求
     * @param addressId 地址ID
     * @return 删除结果
     */
    @DeleteMapping("/{addressId}")
    public ResultVO<Boolean> delete(HttpServletRequest request, @PathVariable Long addressId) {
        Long userId = (Long) request.getAttribute("userId");
        Boolean result = addressService.delete(userId, addressId);
        return ResultVO.success(result);
    }
    
    /**
     * 获取收货地址列表
     *
     * @param request HTTP请求
     * @return 地址列表
     */
    @GetMapping("/list")
    public ResultVO<List<Address>> list(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        List<Address> addressList = addressService.list(userId);
        return ResultVO.success(addressList);
    }
    
    /**
     * 获取收货地址详情
     *
     * @param request   HTTP请求
     * @param addressId 地址ID
     * @return 地址详情
     */
    @GetMapping("/{addressId}")
    public ResultVO<Address> detail(HttpServletRequest request, @PathVariable Long addressId) {
        Long userId = (Long) request.getAttribute("userId");
        Address address = addressService.getDetail(userId, addressId);
        return ResultVO.success(address);
    }
    
    /**
     * 设置默认收货地址
     *
     * @param request   HTTP请求
     * @param addressId 地址ID
     * @return 设置结果
     */
    @PutMapping("/set-default/{addressId}")
    public ResultVO<Boolean> setDefault(HttpServletRequest request, @PathVariable Long addressId) {
        Long userId = (Long) request.getAttribute("userId");
        Boolean result = addressService.setDefault(userId, addressId);
        return ResultVO.success(result);
    }
    
    /**
     * 获取默认收货地址
     *
     * @param request HTTP请求
     * @return 默认地址
     */
    @GetMapping("/default")
    public ResultVO<Address> getDefault(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        Address address = addressService.getDefault(userId);
        return ResultVO.success(address);
    }
} 